<?php
ini_set('display_errors',0);
set_time_limit(0);
include_once  dirname(dirname(dirname(dirname(__FILE__)))).'/mgtv_v2/mgtv_init.php';
define('ORG_ID', 'sihua');
include_once dirname(dirname(dirname(dirname(__FILE__)))) . '/mgtv_v2/'.ORG_ID.'/init.php';
include_once  dirname(dirname(dirname(dirname(__FILE__)))).'/mgtv_v2/'.ORG_ID.'/define.php';

$ip = i_get_ip();
i_write_log_core('notify from '.$ip,'sihua/c2');
$postdata = file_get_contents("php://input");
i_write_log_core('notify data '.$postdata,'sihua/c2');
$xml = $postdata;
$dom = new DOMDocument('1.0', 'utf-8');
$dom -> loadXML($xml);
$xpath = new DOMXPath($dom);
$asset_list = array();
$nns_guid = $xpath->query('/SyncContentsResult')->item(0)->getAttribute('serialNo');
$log_path_temp = '/data/log/mgtv/'.ORG_ID.'/notify/'.date('Ymd');
$log_path = dirname(dirname(dirname(dirname(__FILE__)))).$log_path_temp;
if(!is_dir($log_path)){
	mkdir($log_path,0777,true);
}
$log_path = $log_path.'/'.$nns_guid.'.xml';
file_put_contents($log_path, $xml);
$xmlfile = $log_path_temp.'/'.$nns_guid.'.xml';
$entries = $xpath -> query('/SyncContentsResult/Assets/Asset');
$result = 0;
foreach ($entries as $item) {
	$array = array(
	'ID' => $item -> getAttribute('ID'), 
	'currentID' => $item -> getAttribute('currentID'), 
	'type' => $item -> getAttribute('type'), 
	'op' => $item -> getAttribute('op'), 
	'result' => $item -> getAttribute('result'), 
	'desc' => $item -> getAttribute('desc'), 
	);
	if($item -> getAttribute('result')=='1'){
		$result = -1;
	}
	$asset_list[] = $array;
}
$array = array('nns_id'=>$nns_guid,'data'=>$asset_list,'nns_notify_result'=>$result,'nns_notify_result_url'=>$xmlfile);
i_write_log_core('notify data OK '.var_export($array,true),'sihua/c2');
unset($array['data']);

c2_task_model::save_c2_notify($array);
c2_task_model::save_c2_notify($array);




echo '0|OK';


